<template>
  <view class="content">
    <!-- <button type="primary" @tap="shareInfo">分享</button> -->
  </view>
</template>

<script>
  import $config from './../../config/global.config';
  import share from '@/common/share.js';
  export default {
    props: {},
    data() {
      return {
        title: 'Hello',
        avatarUrl: '',
        invitationCode: '',
        username: '',
        imgUrl: this.$mConfig.staticUrl
      };
    },
    onLoad() {},
    onShow() {},
    onBackPress() {
      //监听back键，关闭弹出菜单
      if (this.shareObj.shareMenu.isVisible()) {
        this.shareObj.shareMenu.hide();
        this.shareObj.alphaBg.hide();
        return true;
      }
    },
    methods: {
      //noHome 代表非首页，即自定义页面
      shareInfo(params, url, title, des, images, noHome = false) {
        // return this.$mUtil.toast('暂无法使用分享功能')
        console.log('params', params, url);
        let shareList = [{
            icon: this.imgUrl + '/sharemenu/wx.png',
            text: '微信好友'
          },
          {
            icon: this.imgUrl + '/sharemenu/pyq.png',
            text: '朋友圈'
          }
        ];

        let shareInfo = {
          href: this.$mConfig.hostUrl + url, // + params,
          title: title || '惠选中亿平台',
          summary: des || '惠选中亿平台',
          imgUrl: images || ''
        };
        /* if (!noHome) {
          let options = params.split('/?')[1].split('&');
          let pageType = options[0].split('=')[1];
          let type = options[1].split('=')[1];
          let model_id = options[2].split('=')[1];
          let model_type = null;

          if (pageType == 3) {
            model_type = 0;
          }
          if (pageType == 2) {
            model_type = 1;
          }
          if (pageType == 1) {
            if (type == 1 || type == 5) {
              model_type = 2;
            }
            if (type == 2) {
              model_type = 5;
            }
            if (type == 3) {
              model_type = 6;
            }
            if (type == 4) {
              model_type = 3;
            }
          }
        } */
        this.shareObj = share(shareInfo, shareList, index => {
          console.log('点击按钮的序号: ' + index);
          console.log('分享信息' + shareInfo.href, shareInfo.title, shareInfo.summary);

          if (index == 0) {
            uni.share({
              provider: 'weixin',
              scene: 'WXSceneSession',
              type: 0,
              href: shareInfo.href,
              title: shareInfo.title,
              summary: shareInfo.summary,
              imageUrl: shareInfo.imgUrl,
              success: res => {
                this.shareObj.shareMenu.hide();
                this.shareObj.alphaBg.hide();
                // this.cacheBbs(model_type, model_id);
                // this.$mUtil.toast('分享成功')
              },
              fail: function(err) {
                this.shareObj.shareMenu.hide();
                this.shareObj.alphaBg.hide();
              }
            });
          } else if (index == 1) {
            uni.share({
              provider: 'weixin',
              scene: 'WXSenceTimeline',
              type: 0,
              href: shareInfo.href,
              title: shareInfo.title,
              summary: shareInfo.summary,
              imageUrl: shareInfo.imgUrl,
              success: res => {
                this.shareObj.shareMenu.hide();
                this.shareObj.alphaBg.hide();
                // this.cacheBbs(model_type, model_id);
              },
              fail: function(err) {
                this.shareObj.shareMenu.hide();
                this.shareObj.alphaBg.hide();
              }
            });
          } else {
            this.shareObj.shareMenu.hide();
            this.shareObj.alphaBg.hide();
          }
        });
        this.$nextTick(() => {
          this.shareObj.alphaBg.show();
          this.shareObj.shareMenu.show();
        });
      },
      cacheBbs(model_type, model_id) {
        console.log('参数为' + model_type, model_id);
        this.$http
          .post(`/opus/cache-bbs`, {
            bbs_type: 2,
            model_type: model_type,
            model_id: model_id
          })
          .then(res => {});
      }
    }
  };
</script>

<style>
  .content {
    text-align: center;
    height: 400upx;
  }

  .logo {
    height: 200upx;
    width: 200upx;
    margin-top: 200upx;
  }

  .title {
    font-size: 36upx;
    color: #8f8f94;
  }
</style>
